package com.jiemengshi.server.controller;

import com.jiemengshi.server.model.DreamReply;
import com.jiemengshi.server.service.AdminDreamService;
import com.jiemengshi.server.service.AdminUserService;
import com.jiemengshi.server.util.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by MingQi on 2017/10/15.
 */
@Controller
@RequestMapping(value = {"/admin/dream"}, produces = {"application/json;charset=UTF-8"})
public class AdminDreamController {
    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private AdminUserService userService;
    
    @Autowired
    private AdminDreamService dreamService;
    
    @ResponseBody
    @RequestMapping("/reply/get")
    public String getDreamReply(HttpServletRequest request, int recordId){
        try {
            userService.getLoginUserId(request);
            
            if(recordId < 1)
                throw new PrjException(ErrorCodeMsg.DREAM_RECORD_ID_ERROR);

            return ApiResult.writeData(dreamService.getDreamReplyByRecordId(recordId));
        } catch (PrjException e){
            return ApiResult.writeException(e);
        } catch (Exception e) {
            log.error(ApiJson.toJson(request.getParameterMap()));
            log.error(PrjException.getTrace(e));
            return ApiResult.SYSTEM_ERROR;
        }
    }
    
    @ResponseBody
    @RequestMapping("/reply/delete")
    public String deleteDreamReply(HttpServletRequest request, int replyId){
        try {
            userService.getLoginUserId(request);
            
            if(replyId < 1)
                throw new PrjException(ErrorCodeMsg.DREAM_RECORD_ID_ERROR);

            if (dreamService.deleteDreamReplyById(replyId))
                return ApiResult.SUCCESS;
            else
                throw new PrjException(ErrorCodeMsg.DREAM_REPLY_DELETE_ERROR);
        } catch (PrjException e){
            return ApiResult.writeException(e);
        } catch (Exception e) {
            log.error(ApiJson.toJson(request.getParameterMap()));
            log.error(PrjException.getTrace(e));
            return ApiResult.SYSTEM_ERROR;
        }
    }

    @ResponseBody
    @RequestMapping("/reply/add/{appName}")
    public String addDreamReply(HttpServletRequest request, DreamReply reply, @PathVariable String appName){
        try {
            reply.setUserAdminId(userService.getLoginUserId(request));
            
            if(reply.getDreamRecordId() < 1)
                throw new PrjException(ErrorCodeMsg.DREAM_RECORD_ID_ERROR);

            if(StringUtil.isEmpty(reply.getContent()))
                throw new PrjException(ErrorCodeMsg.DREAM_REPLY_CONTENT_ERROR);

            if(dreamService.addDreamReply(reply, appName))
                return ApiResult.SUCCESS;
            else
                throw new PrjException(ErrorCodeMsg.DREAM_REPLY_ADD_ERROR);
        } catch (PrjException e){
            return ApiResult.writeException(e);
        } catch (Exception e) {
            log.error(ApiJson.toJson(request.getParameterMap()));
            log.error(PrjException.getTrace(e));
            return ApiResult.SYSTEM_ERROR;
        }
    }

    @ResponseBody
    @RequestMapping("/record/list/time")
    public String listDreamRecordByTime(HttpServletRequest request,
                                        @RequestParam(required = false, defaultValue = "0") int start,
                                        @RequestParam(required = false, defaultValue = "10") int limit){
        try {
            userService.getLoginUserPermissionLevel(request);

            if(start == 0)
                return ApiResult.writeData(dreamService.listDreamRecordByTime(start, limit), dreamService.countDreamRecordByTime());
            else
                return ApiResult.writeData(dreamService.listDreamRecordByTime(start, limit));
        } catch (PrjException e){
            return ApiResult.writeException(e);
        } catch (Exception e) {
            log.error(ApiJson.toJson(request.getParameterMap()));
            log.error(PrjException.getTrace(e));
            return ApiResult.SYSTEM_ERROR;
        }
    }

    @ResponseBody
    @RequestMapping("/record/list/user")
    public String listDreamRecordByUser(HttpServletRequest request,
                                        @RequestParam(required = false, defaultValue = "0") int start,
                                        @RequestParam(required = false, defaultValue = "10") int limit){
        try {
            userService.getLoginUserPermissionLevel(request);

            if(start == 0)
                return ApiResult.writeData(dreamService.listDreamRecordByUser(start, limit), dreamService.countDreamRecordByUser());
            else
                return ApiResult.writeData(dreamService.listDreamRecordByUser(start, limit));
        } catch (PrjException e){
            return ApiResult.writeException(e);
        } catch (Exception e) {
            log.error(ApiJson.toJson(request.getParameterMap()));
            log.error(PrjException.getTrace(e));
            return ApiResult.SYSTEM_ERROR;
        }
    }

    @ResponseBody
    @RequestMapping("/record/list/nickname")
    public String listDreamRecordByUserNickname(HttpServletRequest request,
                                                @RequestParam String nickname,
                                        @RequestParam(required = false, defaultValue = "0") int start,
                                        @RequestParam(required = false, defaultValue = "10") int limit){
        try {
            if(StringUtil.isEmpty(nickname))
                throw new PrjException(ErrorCodeMsg.PARAMETER_ERROR);
            
            userService.getLoginUserPermissionLevel(request);

            if(start == 0)
                return ApiResult.writeData(dreamService.searchDreamRecordByUserName(nickname, start, limit), dreamService.countDreamRecordByNickname(nickname));
            else
                return ApiResult.writeData(dreamService.searchDreamRecordByUserName(nickname, start, limit));
        } catch (PrjException e){
            return ApiResult.writeException(e);
        } catch (Exception e) {
            log.error(ApiJson.toJson(request.getParameterMap()));
            log.error(PrjException.getTrace(e));
            return ApiResult.SYSTEM_ERROR;
        }
    }
}
